package org.qcit.plugin.api;

import java.io.File;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitUtil {
    public static Retrofit getRetrofit(String baseUrl) {
        Retrofit.Builder builder = new Retrofit.Builder();
        builder.baseUrl(baseUrl);
        builder.addConverterFactory(GsonConverterFactory.create());
        OkHttpClient.Builder okhttpBuilder = new OkHttpClient().newBuilder();
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        okhttpBuilder.addInterceptor(logging);
        builder.client(okhttpBuilder.build());
        return builder.build();
    }

    public static RequestBody getTextBody(String text) {
        return RequestBody.create(MediaType.parse("text/plain"),text);
    }

    public static MultipartBody.Part getFilePart(String s, File apkFile) {
        return MultipartBody.Part.createFormData("file",apkFile.getAbsoluteFile().getName()
                ,RequestBody.create(MediaType.parse("multipart/form-data"), apkFile));
    }
}
